package com.duanjw.bank.rate.handler;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.DateFormatUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 工商银行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class GongShangBanRateHandler implements BankRateHandler {
    Map<String, String> headers = new HashMap<>();

    {
        headers.put("Content-Type", "application/json");
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
    }

    @Override
    public BankRate getBankRate() throws IOException {
        List<String> allDate = getAllDate();
        String lastDate = allDate.get(0);

        Connection connect = Jsoup.connect("https://papi.icbc.com.cn/interestRate/deposit/queryRMBDepositInfo?type=CH&date=" + lastDate);
        String responseBody = connect.headers(headers).ignoreContentType(true).execute().body();

        JSONObject responseJson = new JSONObject(responseBody).getJSONObject("data");

        String huoQiCunKuanLiLv = responseJson.getStr("huoQi");

        String zhengCunZhengQu3YueLiLv = responseJson.getStr("zczqThreeMonths");
        String zhengCunZhengQu6YueLiLv = responseJson.getStr("zczqHalfYear");
        String zhengCunZhengQu12YueLiLv = responseJson.getStr("zczqOneYear");
        String zhengCunZhengQu24YueLiLv = responseJson.getStr("zczqTwoYears");
        String zhengCunZhengQu36YueLiLv = responseJson.getStr("zczqThreeYears");
        String zhengCunZhengQu60YueLiLv = responseJson.getStr("zczqFiveYears");

        String lingCunZhengQu12YueLiLv = responseJson.getStr("lczqOneYear");
        String lingCunZhengQu36YueLiLv = responseJson.getStr("lczqThreeYears");
        String lingCunZhengQu60YueLiLv = responseJson.getStr("lczqFiveYears");
        String dingHuoLiangBianMiaoShu = responseJson.getStr("dhlb");

        String dingHuoLiangBian3YueLiLv = null;
        String dingHuoLiangBian6YueLiLv = null;
        String dingHuoLiangBian12YueLiLv = null;
        if (Objects.equals(dingHuoLiangBianMiaoShu, "按一年以内定期整存整取同档次利率打6折")) {
            dingHuoLiangBian3YueLiLv = new BigDecimal(zhengCunZhengQu3YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
            dingHuoLiangBian6YueLiLv = new BigDecimal(zhengCunZhengQu6YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
            dingHuoLiangBian12YueLiLv = new BigDecimal(zhengCunZhengQu12YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        }

        String tongZhiCunKuan1TianLiLv = responseJson.getStr("tzOneDay");
        String tongZhiCunKuan7TianLiLv = responseJson.getStr("tzSevenDay");

        String xieDingCunKuanLiLv = responseJson.getStr("xdck");

        return new BankRate(DateFormatUtil.format3(lastDate), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, xieDingCunKuanLiLv);
    }

    private List<String> getAllDate() throws IOException {
        Connection connect = Jsoup.connect("https://papi.icbc.com.cn/interestRate/deposit/queryRMBDepositDateList?type=CH");
        String responseBody = connect.headers(headers).ignoreContentType(true).execute().body();

        JSONArray responseJson = new JSONObject(responseBody).getJSONArray("data");
        List<String> result = new ArrayList<>();
        for (Object o : responseJson) {
            result.add(o.toString());
        }
        return result;
    }

    @Override
    public String getBankName() {
        return "工商银行";
    }
}
